﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Lab.Client;
using System.Threading;
using Microsoft.TeamFoundation.TestManagement.Client;
using HybridLab.Workflow.Activities.Info;
using HybridLab.Virtual.Interfaces;
using Microsoft.TeamFoundation.Client;

namespace HybridLab.Workflow.Activities.UI
{
	internal class EnvironmentDetailsEx : PropertyErrorManager
	{
		private EventHandler EnvironmentChangedHandler;

		internal HybridLabEnvironmentSnapshotInfo SnapshotInfo { get; set; }
		internal HybridLabHostCredentials Credentials { get; set; }
		internal string HostType { get; set; }
		internal string InteractiveUserPassword { get; set; }

		public EnvironmentDetailsEx()
		{
			SnapshotInfo = new HybridLabEnvironmentSnapshotInfo();
			Credentials = new HybridLabHostCredentials();
		}
		
		internal event EventHandler EnvironmentChanged
		{
			add
			{
				EventHandler handler2;
				EventHandler environmentChanged = EnvironmentChangedHandler;
				do
				{
					handler2 = environmentChanged;
					EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value);
					environmentChanged = Interlocked.CompareExchange<EventHandler>(ref EnvironmentChangedHandler, handler3, handler2);
				}
				while (environmentChanged != handler2);
			}
			remove
			{
				EventHandler handler2;
				EventHandler environmentChanged = EnvironmentChangedHandler;
				do
				{
					handler2 = environmentChanged;
					EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value);
					environmentChanged = Interlocked.CompareExchange<EventHandler>(ref EnvironmentChangedHandler, handler3, handler2);
				}
				while (environmentChanged != handler2);
			}
		}

		private ITestEnvironment environment;
		internal ITestEnvironment Environment
		{
            get
            {
                return environment;
            }
			set
			{
                environment = value;
				if (value == null)
                {
					base.SetPropertyError("Environment", "Please select a standard environment");
                }
                else
                {
					base.ClearPropertyError("Environment");
                }
				base.RaiseEvent(EnvironmentChangedHandler);
			}
		}

		private bool revertToSnapshot;
		internal bool RevertToSnapshot 
		{
			get
			{
				return revertToSnapshot;
			}
			set
			{
				revertToSnapshot = value;
				ValidateHost();
			}
		}


		private string hostName;
		internal string HostName
		{
			get
			{
				return hostName;
			}
			set
			{
				hostName = value;
				ValidateHost();
			}
		}

		private void ValidateHost()
		{
			if (revertToSnapshot && string.IsNullOrEmpty(hostName))
			{
				base.SetPropertyError("Host", "Please select an environment host");
			}
			else
			{
				base.ClearPropertyError("Host");
			}
			base.RaiseEvent(EnvironmentChangedHandler);
		}
	}
}
